<template>
    <div class="setting-drawer" ref="settingDrawer">
        <div class="setting-drawer-index-handle" @click="toggle" :style="iconTheme" v-if="visible">
            <a-icon type="close"/>
        </div>
        <a-drawer
                width="300"
                placement="right"
                @close="onClose"
                :closable="false"
                :visible="visible"
        >
            <div class="setting-drawer-index-content">
                <div :style="{ marginBottom: '24px' }">
                    <h3 class="setting-drawer-index-title">Overall Style Settings</h3>

                    <div class="setting-drawer-index-blockChecbox">
                        <a-tooltip>
                            <template slot="title">
                              Dark Menu Style
                            </template>
                            <div class="setting-drawer-index-item" @click="MenuThemeChange('dark')">
                                <icon-dark></icon-dark>
                                <div class="setting-drawer-index-selectIcon" v-if="navigationTheme === 'dark'">
                                    <a-icon type="check"/>
                                </div>
                            </div>
                        </a-tooltip>

                        <a-tooltip>
                            <template slot="title">
                              Bright Menu Style
                            </template>
                            <div class="setting-drawer-index-item" @click="MenuThemeChange('light')">
                                <icon-light></icon-light>
                                <div class="setting-drawer-index-selectIcon" v-if="navigationTheme !== 'dark'">
                                    <a-icon type="check"/>
                                </div>
                            </div>
                        </a-tooltip>
                    </div>
                </div>

                <div :style="{ marginBottom: '24px' }">
                    <h3 class="setting-drawer-index-title">Theme color</h3>

                    <div style="height: 20px">
                        <a-tooltip class="setting-drawer-theme-color-colorBlock" v-for="(item, index) in colorList"
                                   :key="index">
                            <template slot="title">
                                {{ item.key }}
                            </template>
                            <a-tag :color="item.color" @click="changePrimaryColor(item)">
                                <a-icon type="check" v-if="item.color == primaryColor"></a-icon>
                            </a-tag>
                        </a-tooltip>

                    </div>
                </div>

            </div>
        </a-drawer>
    </div>
</template>

<script>
    import {mapState, mapMutations} from 'vuex';
    import IconDark from "../icon/IconDark";
    import IconLight from "../icon/IconLight";

    const colorList = [
        {
            key: '薄暮', color: '#F5222D'
        },
        {
            key: '火山', color: '#FA541C'
        },
        {
            key: '日暮', color: '#FAAD14'
        },
        {
            key: '明青', color: '#13C2C2'
        },
        {
            key: '极光绿', color: '#52C41A'
        },
        {
            key: '拂晓蓝（默认）', color: '#1890FF'
        },
        {
            key: '极客蓝', color: '#2F54EB'
        },
        {
            key: '酱紫', color: '#722ED1'
        }
    ]

    export default {
        components: {IconLight, IconDark},
        data() {
            return {
                primaryColor: "#1890FF",//主题色
                visible: false,
                drawerWidth: 0,
                colorList,
            }
        },
        computed: {
            ...mapState(['navigationTheme']),
            iconTheme: function () {
                let rightDistance = '0px';
                if (this.visible) {
                    rightDistance = '300px';
                }
                return {backgroundColor: this.primaryColor, right: rightDistance};
                // return { backgroundColor: this.primaryColor};
            }
        },
        watch: {
            primaryColor: function () {
                window.localStorage.setItem('primaryColor', this.primaryColor);
            }
        },
        methods: {
            ...mapMutations(["navigationThemeChange"]),
            showDrawer() {
                this.visible = true;
            },
            onClose() {
                this.visible = false;
            },
            toggle() {
                this.visible = !this.visible;
            },
            changePrimaryColor(item) {

                if (this.primaryColor !== item.color) {
                    this.primaryColor = item.color;
                    window.less.modifyVars({
                        '@primary-color': item.color,
                    }).then(() => this.$message.success("更换主题色成功"))
                        .catch(() => this.$message.warn("更换主题色失败，请联系工程师"));
                }
            },
            MenuThemeChange(theme) {
                this.navigationThemeChange({
                    theme: theme
                });
            }
        },
        mounted() {
            let primaryColor = window.localStorage.getItem('primaryColor');
            if (primaryColor) {
                this.primaryColor = primaryColor;
            }
        }
    }
</script>

<style lang="less" scoped>

    .setting-drawer-index-content {

        .setting-drawer-index-blockChecbox {
            display: flex;

            .setting-drawer-index-item {
                margin-right: 16px;
                position: relative;
                border-radius: 4px;
                cursor: pointer;

                img {
                    width: 48px;
                }

                .setting-drawer-index-selectIcon {
                    position: absolute;
                    top: 0;
                    right: 0;
                    width: 100%;
                    padding-top: 15px;
                    padding-left: 24px;
                    height: 100%;
                    color: #1890ff;
                    font-size: 14px;
                    font-weight: 700;
                }
            }
        }

        .setting-drawer-theme-color-colorBlock {
            width: 20px;
            height: 20px;
            border-radius: 2px;
            float: left;
            cursor: pointer;
            margin-right: 8px;
            padding-left: 0px;
            padding-right: 0px;
            text-align: center;
            color: #fff;
            font-weight: 700;

            i {
                font-size: 14px;
            }
        }
    }

    .setting-drawer-index-handle {
        position: absolute;
        top: 80px;
        background: #1890ff;
        width: 48px;
        height: 48px;
        right: 0px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        pointer-events: auto;
        z-index: 1001;
        text-align: center;
        font-size: 16px;
        border-radius: 4px 0 0 4px;

        i {
            color: rgb(255, 255, 255);
            font-size: 20px;
        }
    }
</style>
